package com.ruoyi.management.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.management.dao.mapper.AmmeterMapper;
import com.ruoyi.management.pojo.param.AmmeterParam;
import com.ruoyi.management.pojo.param.StartAssociationParam;
import com.ruoyi.management.pojo.param.ZtParam;
import com.ruoyi.management.pojo.po.AmmeterResultPo;
import com.ruoyi.management.pojo.vo.*;
import com.ruoyi.management.service.AmmeterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Slf4j
@RequestMapping("/ammeter")
@RestController
@Api(tags = "电表管理接口")
public class AmmeterController extends BaseController {
    @Autowired
    private AmmeterService ammeterService;
    @Autowired
    private AmmeterMapper ammeterMapper;

    @ApiOperation(value = "新增智联器")
    @PostMapping("/selectzl")
    public AjaxResult selectzl(@RequestBody ZtParam ztParam){
        int i = ammeterService.selectzl(ztParam);
        if(i<=0){
            return success("添加失败");
        }
        return success("添加成功");
    }

    @PostMapping("/selectAllZl")
    public AjaxResult selectAllZl(@RequestBody ZtParam ztParam){
        List<ZlVO> zts = ammeterService.selectAllZt(ztParam);
        if (zts.isEmpty()){
            return success("没有相关数据");
        }
        return success(zts);
    }

//    @ApiOperation(value = "新增电表归属载体选择")
//    @PostMapping("/selectzt")
//    public AjaxResult selectzt(@RequestBody ZtParam ztParam){
//        if (ztParam == null) {
//            PageHelper.startPage(1, 20); // 默认第一页
//        } else {
//            PageHelper.startPage(ztParam.getPageNum(), 10);
//        }
//        List<ZTVO> zts = ammeterService.selectzt(ztParam);
//        PageInfo<ZTVO> list = new PageInfo<>(zts);
//        log.debug("查询所有载体供添加列表选择,{}",zts);
//        return success(list);
//    }

    @ApiOperation(value = "新增电表归属载体选择")
    @PostMapping("/selectzt")
    public AjaxResult selectzt(@RequestBody ZtParam ztParam) {
        try {
            List<ZTVO> ztvos = ammeterService.selectZtTree(ztParam);
            if (CollectionUtils.isEmpty(ztvos)) {
                return success("没有相关数据");
            } else {
                PageInfo<ZTVO> list = new PageInfo<>(ztvos);
                return success(list);
            }
        } catch (Exception e) {
            log.error("查询失败: {}", e.toString());
            return error("查询失败");
        }
    }

    @ApiOperation(value = "新增载体")
    @PostMapping("/addZt")
    public AjaxResult addZt(@RequestBody ZtParam ztParam){
        int number=ammeterService.addZt(ztParam);
        if (number>0){
            return success("添加成功");
        }
       return success("添加失败");
    }

    @ApiOperation(value = "修改载体")
    @PostMapping("/updateZt")
    public AjaxResult updateZt(@RequestBody ZtParam ztParam){
        int number=ammeterService.updateZt(ztParam);
        if (number>0){
            return success("修改成功");
        }
        return success("修改失败");
    }

    @ApiOperation(value = "新增电表")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody AmmeterParam ammeterParam){
        log.debug("新增电表",ammeterParam);
        int  number= ammeterService.add(ammeterParam);
        if (number>0){
            return success("添加成功");
        }
        return success("添加失败");
    }

    @ApiOperation(value="显示空闲列表")
    @PostMapping("/selectFreeFilter")
    public AjaxResult selectFreeFilter(@RequestBody AmmeterParam ammeterParam){

        List<AmmeterResultVO> ammeterFreeListVOS= ammeterService.freeListFilter(ammeterParam);
        if (ammeterFreeListVOS.isEmpty()){
            return success("没有相关数据");
        }
        PageInfo<AmmeterResultVO> list = new PageInfo<>(ammeterFreeListVOS);
        return success(list);
    }

    @ApiOperation(value = "选房弹窗")
    @PostMapping("/selectRoom")
    public AjaxResult selectHouse(@RequestBody AmmeterParam ammeterParam){
        List<RoomVO> selectHouse = ammeterService.selectHouse(ammeterParam);
        return success(selectHouse);
    }

    @ApiOperation(value = "企业关联弹窗")
    @PostMapping("/enterprise")
    public AjaxResult enterprise(@RequestBody AmmeterParam ammeterParam){
        List<EnterpriseVO> enterprise = ammeterService.enterprise(ammeterParam);
        return success(enterprise);
    }

    @ApiOperation(value = "日度报表选择初始电量")
    @PostMapping("/startDayPower")
    public AjaxResult startDayPower(@RequestBody AmmeterParam ammeterParam){
        List<AmmeterResultVO> freeList = ammeterService.startDayPower(ammeterParam);
        return success(freeList);
    }

    @ApiOperation(value = "启动关联电表")
    @PostMapping("/startAssociation")
    public AjaxResult startAssociation(@RequestBody StartAssociationParam startAssociationParam){
        boolean i= ammeterService.startAssociation(startAssociationParam);
        if (i){
            return success("关联成功");
        }
        return success("关联失败");
    }

    @ApiOperation(value="显示使用列表")
    @PostMapping("/selectuseFilter")
    public AjaxResult selectuseFilter(@RequestBody AmmeterParam ammeterParam){
        List<AmmeterResultVO> ammeterFreeListVOS= ammeterService.useListFilter(ammeterParam);
        if (ammeterFreeListVOS == null || ammeterFreeListVOS.isEmpty()) {
            return success("没有相关数据");
        }
        PageInfo<AmmeterResultVO> list = new PageInfo<>(ammeterFreeListVOS);
        return success(list);
    }

    @ApiOperation(value = "退用取关")
    @PostMapping("/withdraw")
    public AjaxResult withdraw(@RequestBody StartAssociationParam startAssociationParam){
        boolean  i = ammeterService.withdraw(startAssociationParam);
        if (i){
            return success("退用成功");
        }
        return success("退用失败");
    }

    @ApiOperation(value = "显示关联客户的电表信息")
    @PostMapping("/selectAmmeter1")
    public AjaxResult updateRoom(@RequestBody AmmeterParam ammeterParam){
        log.debug("显示客户关联的电表信息");
        List<AmmeterResultVO> i = ammeterService.updateRoom(ammeterParam);
        if (i==null){
            return success("没有相关数据");
        }
        return success(i);
    }

    @ApiOperation(value = "显示关联客户出租房源")
    @PostMapping("/selectRoom1")
    public AjaxResult selectRoom1(@RequestBody AmmeterParam ammeterParam){
        log.debug("显示关联客户出租房源");
        List<AmmeterResultVO> a=  ammeterService.stqyyData(ammeterParam);
        if (a==null){
            return success("没有相关数据");
        }
        return success(a);
    }

    @ApiOperation(value = "关联房间更新")
    @PostMapping("/updateRoom")
    public AjaxResult updateRoomAA(@RequestBody AmmeterParam ammeterParam){
        log.debug("关联房间更新");
        int i = ammeterService.updateRoomAa(ammeterParam);
        if (i<=0){
            return success("更新失败");
        }
        return success("更新成功");
    }

    @ApiOperation(value = "显示所有电表")
    @GetMapping("/selectAllAmmeter")
    public AjaxResult selectAllAmmeter(
            @RequestParam(name = "dbbh",  required = false)   String dbbh,
            @RequestParam(name = "zlid",  required = false)    String zlid,
            @RequestParam("tgszt") String tgszt,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ){

        String zt = null;
        // 当前账号非admin账号
        if (StringUtils.isNotBlank(tgszt) && !"1".equals(tgszt)) {
            ZtParam ztParam = new ZtParam();
            ztParam.setTgszt(tgszt);
            List<ZTVO> ztvos = ammeterMapper.selectZtByNra(ztParam);
            if (CollectionUtils.isNotEmpty(ztvos)) {
                zt = ztvos.get(0).getNr();
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        List<AmmeterResultVO> ammeterResultVOS = ammeterService.selectAllAmmeter(dbbh,zlid, zt);
        PageInfo<AmmeterResultVO> list = new PageInfo<>(ammeterResultVOS);
        return success(list);
    }

    @ApiOperation(value = "显示智联器详情")
    @GetMapping("/selectzlx")
    public AjaxResult selectzlx(@RequestParam String zlid){
        ZlVO selectzt = ammeterService.selectztx(zlid);
        return success(selectzt);
    }

    @ApiOperation(value = "通过智联器查看关联电表")
    @GetMapping("/selectzldb")
    public AjaxResult selectzldb(@RequestParam String zlid){
        List<AmmeterResultVO> selectzldb = ammeterService.selectzldb(zlid);
        if (selectzldb==null){
            return success("没有相关数据");
        }
        return success(selectzldb);
    }

    @ApiOperation(value = "电表详情(基本信息)")
    @GetMapping("/selectAmmeterx")
    public AjaxResult selectAmmeterx(@RequestParam String dbbh){
        AmmeterResultPo ammeterResultPo=ammeterService.getClosestAmmeterData(dbbh);
        AmmeterXQVO selectAmmeterx = ammeterService.selectAmmeterx(dbbh);
        AjaxResult result = new AjaxResult();
        result.put("实时数据",ammeterResultPo);
        result.put("电表详情",selectAmmeterx);
        return success(result);
    }

    @ApiOperation(value = "计费中电表")
    @GetMapping("/selectMeterList")
    public AjaxResult selectMeterList(
            @RequestParam("date") String date,
            @RequestParam("tgszt")   String tgszt,
            @RequestParam("yxa") Integer yxa,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam("zt") String zt
    ){

        PageHelper.startPage(pageNum, pageSize);
        List<DbysyVO> selectMeterList = ammeterService.selectMeterList(date,tgszt,yxa,zt);
        PageInfo<DbysyVO> list = new PageInfo<>(selectMeterList);
        return success(list);
    }

    @ApiOperation(value = "计费中电表")
    @GetMapping("/switch")
    public AjaxResult switchAmmeter(
            @RequestParam("dbbh") String dbbh,
            @RequestParam("tNumber") Integer tNumber
    ){

        Boolean a = ammeterService.switchAmmeter(dbbh,tNumber);
        if (a){
            return success("开合闸操作成功");
        }
        return success("操作失败");
    }

    @ApiOperation(value = "载体总电量|租户总电量|公共总电量")
    @GetMapping("/selectAllPower")
    public AjaxResult selectAllPower(
            @RequestParam("month") String  month,
            @RequestParam(value = "zt", required = false)    String zt,
            @RequestParam(value = "lc", required = false)    String lc
    ){
        //载体总电量
        BigDecimal  allPower= ammeterService.selectAllPower(month,zt,lc);
        //租户总电量
        BigDecimal  tenantPower= ammeterService.selectTenantPower(month,zt,lc);
        //公共总电量
        BigDecimal  publicPower= ammeterService.selectPublicPower(month,zt,lc);
        //自用总电量
        BigDecimal  selfPower= ammeterService.selectSelfPower(month,zt,lc);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("载体总电量",allPower);
        ajaxResult.put("租户总电量",tenantPower);
        ajaxResult.put("公共总电量",publicPower);
        ajaxResult.put("自用总电量",selfPower);
        return success(ajaxResult);
    }

    @ApiOperation(value = "项目电费数据统计")
    @GetMapping("/selectProjectPower")
    public AjaxResult selectProjectPower(
            @RequestParam("month") String  month,
            @RequestParam(value = "zt", required = false)    String  zt
    ){
            DbkhhkbVO dbkhhkbVO = ammeterService.selectProjectPower(month,zt);
            return success(dbkhhkbVO);
    }

    @ApiOperation(value = "显示租户整体电费缴纳和开票情况")
    @GetMapping("/selectTenantPower")
    public AjaxResult selectTenantPower(
            @RequestParam(value = "zt", required = false)    String  zt,
            @RequestParam(value = "qymc", required = false) String  qymc,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<DbkhhsjbVO> dbkhhsjbVO = ammeterService.selectTenantPower1(qymc,zt);
        if (dbkhhsjbVO==null || dbkhhsjbVO.isEmpty()){
            return success("没有相关数据");
        }
        PageInfo<DbkhhsjbVO> list = new PageInfo<>(dbkhhsjbVO);
            return success(list);
    }

    @ApiOperation(value = "修改租户初始电量")
    @GetMapping("/startPower")
    public AjaxResult startPower(
            @RequestParam("dbbh") String dbbh,
            @RequestParam("dbz") BigDecimal dbz,
            @RequestParam("timestamp")
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            Date timestamp
    ){
        Boolean a = ammeterService.startPower(dbbh,dbz,timestamp);
        if (!a){
            return AjaxResult.success("修改失败");
        }
        return AjaxResult.success("修改成功");
    }

    @ApiOperation(value = "修改电表用途")
    @GetMapping("/updateYt")
    public AjaxResult updateYt(
            @RequestParam("dbbh") String dbbh,
            @RequestParam("dbyt") String dbyt
    ){
        Boolean a = ammeterService.updateYt(dbbh,dbyt);
        if (!a){
            return AjaxResult.success("修改失败");
        }
        return AjaxResult.success("修改成功");
    }

}
